import 'package:expandable_richtext/expandable_rich_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../assets/styles.dart';
import '../../../../model/advertisements/ad_resp_model.dart';
import '../../../../model/play/video_detail_model.dart';
import '../../../../services/app_service.dart';
import '../../../../utils/color.dart';
import '../../../../utils/enum.dart';
import '../../../../utils/extension.dart';
import '../../../../utils/initAdvertisementInfo.dart';

///
/// 左边文字区域
///
class ShortVPDescArea extends StatefulWidget {
  final VideoDetail detail;
  final VoidCallback? onTapBuy;

  const ShortVPDescArea(this.detail, {super.key, this.onTapBuy});

  @override
  State<StatefulWidget> createState() => _State();
}

class _State extends State<ShortVPDescArea> {
  VideoDetail get detail => widget.detail;

  Widget _buildAd(AdvertisementInfos ad) {
    return Obx(() {
      if (Get.find<AppService>().shortShowAd) {
        return Container(
          decoration: BoxDecoration(
            color: COLOR.black.withValues(alpha: 0.5),
            borderRadius: Styles.borderRadius.all(4.w),
          ),
          padding: EdgeInsets.symmetric(horizontal: 7.w, vertical: 1.w),
          alignment: Alignment.center,
          child: Text(
            ad.adName ?? '',
            style: TextStyle(fontSize: 13.w, fontWeight: FontWeight.w500),
          ),
        );
      }
      return const SizedBox.shrink();
    });
  }

  Widget _buildTitle() {
    return ExpandableRichText(
      detail.title ?? '',
      style: TextStyle(
        fontSize: 14.w,
        fontWeight: FontWeight.w400,
      ),
      expandText: '展开',
      collapseText: '收起',
      maxLines: 2,
      toggleTextStyle: const TextStyle(
        fontWeight: FontWeight.w600,
      ),
    );
  }

  Widget _buildTags() {
    if (detail.tags?.isNotEmpty != true) {
      return const SizedBox.shrink();
    }
    return Wrap(
      spacing: 10.w,
      children: detail.tags!
          .map((e) => Text(
                '#${e.tagsTitle}',
                style: TextStyle(
                  fontSize: 13.w,
                  fontWeight: FontWeight.w500,
                ),
              ).onTap(() {}))
          .toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    final ad =
        initSequenceAdvertisementInfo(AdPlaceNameEnum.SV_BOTTOM_BANNER_APP)
            .firstOrNull;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        if (ad != null) ...[
          _buildAd(ad),
          10.verticalSpaceFromWidth,
        ],
        _buildTitle(),
        if (detail.tags?.isNotEmpty == true) ...[
          10.verticalSpaceFromWidth,
          _buildTags(),
        ]
      ],
    );
  }
}
